<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        window.onload = function() {
            document.getElementById('username').onblur = function() {
                // console.log('object');
                var username = document.getElementById('username').value;
                var pwd = document.getElementById('pwd').value;

                //1,发送XMLrequest对象
                var xhr = null;
                if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                } else {
                    xhr = new ActiveXObject('Microsoft.XMLHTTP');
                }
                //2,准备发送
                var parm = "username=" + username + "&password=" + pwd;
                //encodeURI 对中文参数进行编码，防止乱码
                xhr.open('get', './ajax2.php?' + encodeURI(parm));
                //3,执行行发送
                xhr.send(null);
                //4,执行回调函数
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            var span = document.getElementById('info');
                            span.innerHTML = xhr.responseText;
                        }
                    }
                }
            }
        }
    </script>
</head>

<body>
    <form>
        姓名:<input type="text" id="username"><span id="info"></span><br> 密码:
        <input type="password" id="pwd"><br>

        <input type="button" value="注册" id="btn">
    </form>
</body>

</html>